﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using TaobaoDistrSales.Common;
using TaobaoDistrSales.Model; 

namespace TaobaoDistrSales
{
    public partial class MainForm  : Form
    {
        #region Ctor

        public MainForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Form Load

        private void MainForm_Load(object sender, EventArgs e)
        {
            using (taobaodbEntities entities = ResourceManager.LoadEntity())
            {
                OptionsConfig config = entities.OptionsConfig.FirstOrDefault();
                bool showMax = config != null ? config.showmax : false;
                this.WindowState = showMax
                    ? FormWindowState.Maximized
                    : FormWindowState.Normal;
                if (WindowState == FormWindowState.Normal)
                {
                    Width = 800;
                    Height = 600;
                }
            }
        }

        #endregion

        #region Menu Events

        private void MainOperationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MainOperationForm mainOperationForm = OpenDockForm<MainOperationForm>("mainOperationForm");
            mainOperationForm.Show();
        }

        private void ShopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShopForm shopForm = OpenDockForm<ShopForm>("shopForm");
            shopForm.Show();
        }

        private void OptionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OptionsForm optionsForm = OpenDockForm<OptionsForm>("optionsForm");
            optionsForm.Show();
        }
        
        #endregion

        #region Dock Content Tab Content Menu

        private void CloseDockContentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DockContent content = MainDockPanel.Contents.FirstOrDefault(c =>
            {
                DockContent con = c as DockContent;
                return con.IsActivated;
                
            }) as DockContent;

            if (content != null)
            {
                content.Close();
            }
        }

        #endregion

        #region Tool Strip

        private void StartToolStripButton_Click(object sender, EventArgs e)
        { 
        }

        private void StopToolStripButton_Click(object sender, EventArgs e)
        {

        }

        #endregion

        #region Assist Method

        private T OpenDockForm<T>(string name)
            where T : DockContent, new()
        {
            T content = MainDockPanel.Contents.FirstOrDefault(c =>
            {
                DockContent con = c as DockContent;
                return c.GetType() == typeof(T) && con.Name == name;
            }) as T;
            if (content == null)
            {
                content = new T();
                content.DockPanel = MainDockPanel;
                content.Dock = DockStyle.Fill;
                content.DockAreas = DockAreas.Document;
                content.TabPageContextMenuStrip = DockContentHeaderContextMenuStrip;
                content.Name = name;
            }
            return content;
        }

        
        #endregion

        #region Session
        private void sessionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("更新SessionKey必须关闭其他窗口,您确认要继续更新吗？", "确认更新SessionKey", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                var contents = MainDockPanel.Contents.ToList();
                foreach (var c in contents)
                {
                    var con = c as DockContent;
                    if (con.Name != "refreshSessionForm" && !(c is RefreshSessionForm))
                    {
                        con.Close();
                    }
                }
                var refreshSessionForm = OpenDockForm<RefreshSessionForm>("refreshSessionForm");
                refreshSessionForm.Show();
            }
        }
        #endregion

    }
}
